//
//  MSNavViewController.swift
//  SwiftWeibo
//
//  Created by 牟松 on 16/8/1.
//  Copyright © 2016年 DoNews. All rights reserved.
//

import UIKit

class MSNavViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationBar.isHidden = true
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        // 根据控制器的数量确定push的是第几层
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            
            if let vc = viewController as? MSBaseViewController {
                var leftBarButtonTitle = "返回"
                if childViewControllers.count == 1 { // 如果是第一次push显示上一个控制器的名字，后面的push返回按钮的名字就是“返回”
                    
                    leftBarButtonTitle = childViewControllers.first?.title ?? "返回"

                }

                vc.navItem.leftBarButtonItem = UIBarButtonItem(MS_BarButtonItem: leftBarButtonTitle, imageName: "navigationbar_back_withtext", selectedImageName:"navigationbar_back_withtext_highlighted", target: self, action: #selector(backButtonDidClick))
                
            }
        }
        
        super.pushViewController(viewController, animated: true)
    }
    
    // MARK: - 监控点击事件
    // 返回按钮
    @objc private func backButtonDidClick() {
        popViewController(animated: true)
    }

}
